import server from '../../server/api'
import { Indicator } from 'mint-ui';
// 请求购物车列表接口
const getShoppingList = ({
	commit,
	state
}, {
	memberId
}) => {
	server.post("/index/cart/cart_list", {
		member_id: memberId
	}, function(res) {

		if(res.data == null) {
			return
		}
		commit("setShoppingList", res.data)
	})
}
// 请求购物车详情接口
const getShoppingDetail = ({
	commit,
	state,
	dispatch
}, {
	goods_id
}) => {
	Indicator.open({
		text: '加载中...',
		spinnerType: 'fading-circle'
	});
	server.post("/index/goods/goods_detail", {
		goods_id
	}, function(res) {

		if(res.error == 0) {
			Indicator.close();
			commit("setShoppingDetail", res.data)
			dispatch('getMsgDataAsyn',{getMsgType: 'goods'});
		}
	})
}
// 购物车选择商品，跳转详情页
const getSelectGoods = ({
	commit,
	state
}, paramas) => {
	const goodsInfo = paramas.data;
	//				const userInfo = wx.getStorageSync('userInfo');

	let param;
	if(paramas.from == "goodsDetails") {
		if(paramas.chooseBuyMethod == 1) {
			// 选择月定制卖法
			let sum = 0;
			for(var i = 0; i < goodsInfo.serializeMonth.length; i++) {
				sum += goodsInfo.serializeMonth[i].count;
				goodsInfo.serializeMonth[i].num = goodsInfo.serializeMonth[i].count
			}
			param = {
				order_label: paramas.chooseBuyMethod,
				member_id: 101076,
				goods_id: goodsInfo.goods_id,
				sp_value_id: goodsInfo.spec[goodsInfo.defaultSpecIndex].sp_value_id,
				sum: sum,
				monthlist: JSON.stringify(goodsInfo.serializeMonth)
			};

		} else if(paramas.chooseBuyMethod == 0) {
			param = {
				member_id: 101076,
				order_label: paramas.chooseBuyMethod,
				ifcart: 'collocation',
				collocation_list: JSON.stringify(goodsInfo)
			}
		} else {

			//拼团，众筹
			param = {
				member_id: 101076,
				order_label: paramas.chooseBuyMethod,
				goods_id: goodsInfo[0].goods_id,
				sp_value_id: goodsInfo[0].sp_value_id,
				num: goodsInfo[0].num,
			}
		}
	} else {
		//从购物车选择商品
		param = paramas
	}
	Indicator.open({
		text: '加载中...',
		spinnerType: 'fading-circle'
	});
	server.post("/index/buy/buy_step1", param, function(res) {

		if(res.error == 0) {
			Indicator.close();
		}
		commit("setSelectGoods", {shoppingSelectGoods:res.data,param})
	})
}
export default {
	getShoppingList,
	getShoppingDetail,
	getSelectGoods
}